﻿#if NETSTANDARD2_0_OR_GREATER || NET5_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;

using zijian666.Abstractions;
using zijian666.Convertor.Base;
using zijian666.Core;

namespace zijian666.Json.standard20;
public class JsonDocumentConvertor : AllowNullConvertor<JsonDocument>
                                   , IFrom<JsonElement, JsonDocument>
                                   , IFrom<string, JsonDocument>
                                   , IFrom<object, JsonDocument>
{

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ConvertResult<JsonDocument> From(IConvertContext context, JsonElement input)
    {
        return JsonDocument.Parse(input.GetRawText());
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ConvertResult<JsonDocument> From(IConvertContext context, string input)
    {
        return JsonDocument.Parse(input);
    }

    public ConvertResult<JsonDocument> From(IConvertContext context, object input)
    {
#if NET6_0_OR_GREATER
        return JsonSerializer.SerializeToDocument(input);
#else
        var json = JsonSerializer.Serialize(input);
        return From(context, json);
#endif
    }
}
#endif